﻿<%@ WebHandler Language="C#" Class="OrderHandler" %>

using System;
using System.Web;
using System.Reflection;
using System.Web.SessionState;
using System.Data;
using System.Collections.Generic;
using WxPayAPI;
using Dos.Model;

public class OrderHandler : IHttpHandler, IRequiresSessionState
{

    public void ProcessRequest(HttpContext context)
    {
        if (context.Request["method"] != null)
        {
            lock (this)
            {
                MethodInfo mi = this.GetType().GetMethod(context.Request["method"]);
                mi.Invoke(this, new object[] { context });
            }
        }
    }

    public void FuturePay(HttpContext context)
    {
        if (context.Session["user"] != null)
        {
            Json u = context.Session["user"].ToString();

            temple_order o = new temple_order();

            if (context.Request.UserAgent.Contains("MicroMessenger"))
            {
                o.OpenID = u["openid"];
                o.NickName = u["nickname"];
                o.HeadImage = u["headimgurl"];
                o.Number = GetRandomNum();
                o.Flag = 3;
                o.Amount = 1M;
                o.RallyID = int.Parse(context.Request["id"]);
                o.Jie = context.Request["jie"];
                o.Yang = context.Request["yang"];

                ManageBase.Add(o);


                string openid = o.OpenID;
                string total_fee = Convert.ToInt32((o.Amount * 100)).ToString();
                //total_fee = "1";
                string body = "法务登记-往生";

                JsApiPay jsApiPay = new JsApiPay();
                jsApiPay.openid = openid;
                jsApiPay.total_fee = int.Parse(total_fee);
                jsApiPay.out_trade_no = o.Number;
                jsApiPay.body = body;

                WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult();
                Json r = jsApiPay.GetJsApiParameters();
                r["Number"] = o.Number;
                r["OrderID"] = o.ID;
                context.Response.Write(r);
            }
            else
            {
                context.Response.Write(o.ID);
            }
        }
    }

    public void BornPay(HttpContext context)
    {
        if (context.Session["user"] != null)
        {
            Json u = context.Session["user"].ToString();

            temple_order o = new temple_order();

            if (context.Request.UserAgent.Contains("MicroMessenger"))
            {
                o.OpenID = u["openid"];
                o.NickName = u["nickname"];
                o.HeadImage = u["headimgurl"];
                o.Number = GetRandomNum();
                o.Flag = 2;
                o.Amount = 1M;
                o.RallyID = int.Parse(context.Request["id"]);
                o.Name = context.Request["name"];

                ManageBase.Add(o);


                string openid = o.OpenID;
                string total_fee = Convert.ToInt32((o.Amount * 100)).ToString();
                //total_fee = "1";
                string body = "法务登记-诞生";

                JsApiPay jsApiPay = new JsApiPay();
                jsApiPay.openid = openid;
                jsApiPay.total_fee = int.Parse(total_fee);
                jsApiPay.out_trade_no = o.Number;
                jsApiPay.body = body;

                WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult();
                Json r = jsApiPay.GetJsApiParameters();
                r["Number"] = o.Number;
                r["OrderID"] = o.ID;
                context.Response.Write(r);
            }
            else
            {
                context.Response.Write(o.ID);
            }
        }
    }

    public void MeritsPay(HttpContext context)
    {
        if (context.Session["user"] != null)
        {
            Json u = context.Session["user"].ToString();

            temple_order o = new temple_order();

            if (context.Request.UserAgent.Contains("MicroMessenger"))
            {
                o.OpenID = u["openid"];
                o.NickName = u["nickname"];
                o.HeadImage = u["headimgurl"];
                o.Number = GetRandomNum();
                o.Flag = 1;
                o.RallyID = int.Parse(context.Request["tid"]);
                o.Amount = decimal.Parse(context.Request["amount"]);

                ManageBase.Add(o);


                string openid = o.OpenID;
                string total_fee = Convert.ToInt32((o.Amount * 100)).ToString();
                //total_fee = "1";
                string body = "寺院功德";

                JsApiPay jsApiPay = new JsApiPay();
                jsApiPay.openid = openid;
                jsApiPay.total_fee = int.Parse(total_fee);
                jsApiPay.out_trade_no = o.Number;
                jsApiPay.body = body;

                WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult();
                Json r = jsApiPay.GetJsApiParameters();
                r["OrderID"] = o.ID;
                context.Response.Write(r);
            }
            else
            {
                context.Response.Write(o.ID);
            }
        }
    }

    public void DaysPay(HttpContext context)
    {
        if (context.Session["user"] != null)
        {
            Json u = context.Session["user"].ToString();

            temple_Days o = new temple_Days();

            if (context.Request.UserAgent.Contains("MicroMessenger"))
            {
                o.OpenID = u["openid"];
                o.NickName = u["nickname"];
                o.HeadImage = u["headimgurl"];
                o.Number = GetRandomNum();
                o.Amount = 0.01M;
                ManageBase.Add(o);


                string openid = o.OpenID;
                string total_fee = Convert.ToInt32((o.Amount * 100)).ToString();
                //total_fee = "1";
                string body = "每日一善";

                JsApiPay jsApiPay = new JsApiPay();
                jsApiPay.openid = openid;
                jsApiPay.total_fee = int.Parse(total_fee);
                jsApiPay.out_trade_no = o.Number;
                jsApiPay.body = body;

                WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult();
                Json r = jsApiPay.GetJsApiParameters();
                r["DayID"] = o.ID;
                context.Response.Write(r);
            }
            else
            {
                context.Response.Write(o.ID);
            }
        }
    }

    // 产生不重复的随机数
    protected string GetRandomNum()
    {
        string str = "";
        Random random = new Random(4 * unchecked((int)DateTime.Now.Ticks));
        str = random.Next(0, 9) + "" + random.Next(0, 9) + "" + random.Next(0, 9) + "" + random.Next(0, 9);
        str = Convert.ToString(DateTime.Now.ToString("yyyyMMddHHmmss")) + str;
        return str;
    }


    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}